#pragma once

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <pthread.h>
#include <unistd.h>
#include <ctime>

struct ThreadInfo
{
    pthread_t _tid;
    std::string ThreadName;
};

static int defaultnum = 5;

template <class T>
class ThreadPool
{
public:
    void Lock()
    {
        pthread_mutex_lock(&_mutex);
    }

    void UnLock()
    {
        pthread_mutex_unlock(&_mutex);
    }

    void ThreadSleep()
    {
        pthread_cond_wait(&_cond, &_mutex);
    }

    bool IsEmpty()
    {
        return _task.empty();
    }

    std::string GetThreadName(pthread_t tid)
    {
        for (auto &it : _threads)
        {
            if (it._tid == tid)
            {
                return it.ThreadName;
            }
        }
        return "NONE";
    }

    void Wakeup()
    {
        pthread_cond_signal(&_cond);
    }

public:
    static void *myhander(void *args)
    {
        ThreadPool<T> *tp = static_cast<ThreadPool<T> *>(args);
        std::string name = tp->GetThreadName(pthread_self());

        while (true)
        {
            tp->Lock();
            while (tp->IsEmpty())
            {
                tp->ThreadSleep();
            }
            T ret = tp->Pop();
            tp->UnLock();

            ret();
        }
    }

    T Pop()
    {
        T t = _task.front();
        _task.pop();
        return t;
    }

    void Push(const T &t)
    {
        Lock();
        _task.push(t);
        Wakeup();
        UnLock();
    }

    void Start()
    {
        int num = _threads.size();
        for (int i = 0; i < num; i++)
        {
            _threads[i].ThreadName = "thread-" + std::to_string(i + 1);
            pthread_create(&(_threads[i]._tid), nullptr, myhander, this);
        }
    }

    //懒汉模式
    static ThreadPool<T>* GetInstance()
    {
        if(_tp == nullptr) //我们发现，只有第一个线程进入的时候_tp才会为空，这样的话后面如果多次进行加锁判断释放锁会降低效率
        {                  //所以这里得二次判断，保证后面不为空的时候直接返回，并且即使第一次有多个线程进入了235行，但是
                           //依然是只会有一个线程成功申请锁并进入if，new空间，其余线程即使后来申请到锁了，但是if判断失效，所以就
                           //会释放锁，然后后来的线程就会在第一个if那里都进不去，进而增加效率
            pthread_mutex_lock(&_lock);
            if(_tp == nullptr)
            {
                _tp = new ThreadPool<T>;
            }
            pthread_mutex_unlock(&_lock);
        }

        return _tp;
    }

private:
    ThreadPool(int num = defaultnum)
        : _threads(num)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }
    ThreadPool(const ThreadPool<T>& copy) = delete;
    const ThreadPool<T>& operator=(const ThreadPool<T>& copy) = delete;
private:
    std::vector<ThreadInfo> _threads;
    std::queue<T> _task;

    pthread_mutex_t _mutex;
    pthread_cond_t _cond;

    static ThreadPool<T>* _tp;
    // static ThreadPool<T>* _tp = nullptr;
    static pthread_mutex_t _lock;
};

template <class T>
ThreadPool<T>* ThreadPool<T>::_tp = nullptr;

template <class T>
pthread_mutex_t ThreadPool<T>::_lock = PTHREAD_MUTEX_INITIALIZER;

